import {Component, OnInit} from '@angular/core';
import {HttpService} from '@core/services/http.service';
import swal from 'sweetalert2';


@Component({
    selector: 'app-table-full',
    templateUrl: './full.component.html'
})
export class TableFullComponent implements OnInit {

    _value;
    data = [];
    ps = 10;
    pi = 1;
    loading = false;
    curRows = [];
    ip = this.http.IP_PORT;
    style: any = {
        width: '70%',
        top: '190px'
    };
    style1: any = {
        width: '40%',
        top: '190px'
    };

    allChecked = false;
    indeterminate = false;
    selectedRows = [];
    totalCallNo = 0;
    Total;
    key = '';

    majorId = '';
    isRemove = false;
    removeMajorName;

    showNewMajor = false;
    majorName;
    classArrList = [];

    showEditClass = false;
    classDataList = [];

    tecModal = false;
    tecList;
    glTecList = [];
    geTecId = [];

    claId
    stuModal = false;
    selectClass = '';
    classList = [];
    stuList =[];

    sch_id = JSON.parse(localStorage.getItem('_user')).sch_id;

    constructor(public http: HttpService) {

    }

    ngOnInit() {
        this.getData();
    }

    getData() {
        this.loading = true;
        let majparam = {
            key: this.key,
            pi: this.pi,
            ps: this.ps,
            sch_id:this.sch_id
        };

        this.http.getMajorList(majparam).subscribe(res => {
            this.data = res['list'];
            this.Total = res['total'];
            this.key = '';
            this.loading = false;
        });
    }

    checkAll(value: boolean) {
        this.curRows.forEach(i => {
            if (!i.disabled) i.checked = value;
        });
        this.refreshStatus();
    }

    refreshStatus() {
        const allChecked = this.curRows.every(value => value.disabled || value.checked);
        const allUnChecked = this.curRows.every(value => value.disabled || !value.checked);
        this.allChecked = allChecked;
        this.indeterminate = (!allChecked) && (!allUnChecked);
        this.selectedRows = this.data.filter(value => value.checked);
        this.totalCallNo = this.selectedRows.reduce((total, cv) => total + cv.callNo, 0);
    }

    dataChange(res: any) {

        this.curRows = res;
        this.refreshStatus();
    }

    pageChange(pi: number) {

        this.pi = pi;
        this.loading = true;
        this.getData();
        this.loading = false;
    }

    doSearch() {
        if (this._value) {
            this.key = this._value;
            this.getData();
        } else {
            this.key = '';
            this.getData();
        }
        // console.log(this._value);
    }

    addMajor() {
        this.majorId = '';
        this.classArrList = [{className: '', classTotal: '', cla_id: ''}];
        this.showNewMajor = true;
    }

    addMajorClass() {
        this.classArrList.push({className: '', classTotal: '', cla_id: ''});
        //console.log(this.classArrList);
    }

    newMajor() {
        if (!this.majorName) {
            swal('专业名称不能为空', '', 'info');
            return;
        }
        for (let ds of this.classArrList) {
            if (!ds.className || !ds.classTotal) {
                swal('班级名称、人数不能为空', '', 'info');
                return;
            }
        }
        let param = {
            major_name: this.majorName,
            class_info: this.classArrList,
            maj_id: this.majorId,
            sch_id:this.sch_id
        };

        this.http.saveMajor(param).subscribe(res => {
            //console.log(res);
            swal(res['msg'],'','info');
            this.getData();
            this.showNewMajor = false;
        });
    }

    edit(data) {
        this.showNewMajor = true;
        this.majorId = data.maj_Id;
        this.classArrList = [];
        let param = {
            maj_id: this.majorId
        };
        this.http.getMajorInfo(param).subscribe(res => {
            //console.log(res);
            this.majorName = res['major'].major_name;
            for (let list of res['list']) {
                //console.log(list);
                this.classArrList.push({className: list.cla_name, classTotal: list.count, cla_id: list.cla_id});
            }
            //console.log(this.classArrList);
        });
    }

    removeMajor() {
        let param = {
            maj_id: this.majorId
        };
        this.http.deleteMajor(param).subscribe(res => {
            //console.log(res);
            swal(res.msg, '', 'info');
            this.isRemove = false;
            this.majorId = '';
            this.getData();
        });
    }

    isDelTec(data) {
        this.isRemove = true;
        this.removeMajorName = data.major_name;
        this.majorId = data.maj_Id;
    }

    getClassList (){
        let param = {
            maj_id:this.majorId
        };
        this.http.getClassList(param).subscribe(res=>{
            //console.log(res);
            this.classDataList = res['list'];
            for(let list of this.classDataList){
                list['isChange'] = true;
                list['isNew'] = false;
            }
        });
        this.getData()
    }

    showClassTotal(data) {
        this.showEditClass = true;
        this.majorId = data.maj_Id;
        this.majorName = data.major_name;
        this.getClassList();
    }

    addClassData(){
        this.classDataList.push({cla_id: '', cla_name: '',count: '', isChange: false,isdelete: 0,maj_id: this.majorId,isNew:true})
    }

    saveClassData(data){
        if(!data.cla_name || !data.count){
            swal('班级名称、人数不能为空','','info');
            return
        }
        if(isNaN(data.count)){
            swal('人数只能填写数字','','info');
            return
        }else{

        }
        this.http.saveClass(data).subscribe(res=>{
            swal(res['msg'],'','info');
            this.getClassList();
        })
    }

    cencalSaveClass(data,i){
        if(data.isNew){
            this.classDataList.splice(i,1)
        }else{
            data.isChange = true;
            this.getClassList();
        }
    }

    removeClassData(data){
        this.http.deleteClass({cla_id:data.cla_id}).subscribe(res=>{
            //console.log(res)
            swal(res['msg'],'','info');
            this.getClassList();
        })
    }

    showTeaModal(data){
        //console.log(data);
        this.tecModal = true;
        this.majorName = data.major_name;
        this.majorId = data.maj_Id;
        let param = {
            key:'',
            pi:'1',
            ps:'9999',
            sch_id:this.sch_id
        };

        this.http.getTeacherList(param).subscribe(res=>{
           // console.log(res);
            this.tecList = res['list'];

            let params = {
                maj_id:this.majorId
            };
            this.http.getTeacherMajor(params).subscribe(res=>{
                //console.log(res);
                let checkTecList = res['list'];
                for(let teclist of this.tecList){
                    teclist['ischeck'] = false;
                    for(let ctList of checkTecList){
                        if(teclist.id == ctList.tea_id){
                            teclist.ischeck = true;
                            this.glTecList.push(teclist.UserName);
                            this.geTecId.push(teclist.id);
                        }
                    }
                }
            });


        })
    }

    tecChange($event,tec){
        //console.log(tec)
        if ($event.target.checked) {
            this.glTecList.push(tec.UserName);
            this.geTecId.push(tec.id);
        } else {
            this.removeByValue(this.glTecList,tec.UserName);
            this.removeByValue(this.geTecId,tec.id)
        }
    }

    saveTeacherMajor(){
        let param = {
            maj_id:this.majorId,
            techarr:this.geTecId
        };
        this.http.addTeacherMajor(param).subscribe(res=>{
            this.getData();
            swal(res['msg'],'','info');
            this.closeTecModal()
        })
    }

    removeByValue(arr, val) {
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == val) {
                arr.splice(i, 1);
                break;
            }
        }
    }

    closeTecModal(){
        this.tecModal = false;
        this.glTecList = [];
        this.geTecId = [];
        this.majorId = ''
    }

    clearMajor() {
        this.showNewMajor = false;
        this.majorName = '';
        this.classArrList = [];
        this.majorId = '';
    }

    showStuTotal(data){
        //console.log(data);
        this.stuModal = true;
        this.http.getClassList({maj_id:data.maj_Id}).subscribe(res=>{
            this.classList = res['list'];
        })
    }

    claChange(event){
       for(let list of this.classList){
           if(list.cla_name == event){
               this.claId = list.cla_id;
               this.http.getStudentByClaId({cla_id:list.cla_id}).subscribe(res=>{
                   this.stuList = res['list']

               })
           }
       }
    }

    isDelStu(stu){
        this.http.deleteStudent({u_id:stu.Id}).subscribe(res=>{
            //console.log(res);
            swal(res['msg'], '', 'info');
            if(res['code']==0){
                this.http.getStudentByClaId({cla_id: this.claId}).subscribe(res => {
                    //console.log(res);
                    this.stuList = res['list'];
                });
            }
        })
    }
}
